<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 权限检测，更新方法都在这。
 *
 * @ auth yunyun
 */

class Api_lib {
    
    var $CI	= NULL;
	var $_main_url = NULL;
	
	var $api_type = "api";
	var $return_type = "json"; //json string
	var $is_array = true; //array,object	
	
	
    function __construct($config = array()) 
    {
    	$this->CI = & get_instance();
		
		//
		$this->CI->load->library("send_lib");
		
		$this->site_url($this->api_type);
		
		
    	if (count($config) > 0)
		{
			$this->initialize($config);
		}
		
		log_message('debug', "api_lib Class Initialized.");
    }
	
	
	public function get($uri = "", $data_arr = array(), $header = array())
	{
		$url = $this->_main_url . ltrim($uri,'/');
		
		$header = $this->header_check($header);
		
		$data_ret = $this->CI->send_lib->get($url,$data_arr,$header);
		
		log_message("info","get:\r\napi_url:".$url."\r\napi_data:".print_r($data_arr,true)."\r\napi_header:".print_r($header,true)."api_lib_return:\r\n".print_r($data_ret,true));
		
		if($this->return_type == "json")
		{
			$ret_arr = json_decode($data_ret,$this->is_array);
			if(is_null($ret_arr))
			{
				
				if($this->is_array)
				{
					$ret_arr = null;
					$ret_arr['type'] = 404;
					$ret_arr['message'] = $data_arr;
					$ret_arr['data']  = "";
				}
				else
				{
					$ret_arr = new stdClass();
					$ret_arr->type = 404;
					$ret_arr->message = $data_arr;
					$ret_arr->data  = "";
				}
				log_message("info","json:".print_r($ret_arr,true));
				return $ret_arr;
				
			}
			else
			{
				log_message("info","json:".print_r($ret_arr,true));
				return $ret_arr;
			}
		}
		else
		{
			log_message("info","other:".print_r($ret_arr,true));
			return $data_arr;
		}
	}
	
	public function post($uri = "", $data_arr = array(), $header = array())
	{
		$url = $this->_main_url . ltrim($uri,'/');
		
		$header = $this->header_check($header);
		
		$data_ret = $this->CI->send_lib->post($url,$data_arr,$header);
		
		log_message("info","post:\r\napi_url:".$url."\r\napi_data:".print_r($data_arr,true)."\r\napi_header:".print_r($header,true)."api_lib_return:\r\n".print_r($data_ret,true));
		
		if($this->return_type == "json")
		{
			$ret_arr = json_decode($data_ret,$this->is_array);
			if(is_null($ret_arr))
			{
				
				if($this->is_array)
				{
					$ret_arr = null;
					$ret_arr['type'] = 404;
					$ret_arr['message'] = $data_arr;
					$ret_arr['data']  = "";
				}
				else
				{
					$ret_arr = new stdClass();
					$ret_arr->type = 404;
					$ret_arr->message = $data_arr;
					$ret_arr->data  = "";
				}
				log_message("info","json:".print_r($ret_arr,true));
				return $ret_arr;
				
			}
			else
			{
				log_message("info","json:".print_r($ret_arr,true));
				return $ret_arr;
			}
		}
		else
		{
			log_message("info","other:".print_r($ret_arr,true));
			return $data_arr;
		}
	}
	
	public function data($uri = "", $data_arr = array(), $header = array())
	{
		$url = $this->_main_url . ltrim($uri,'/');
		
		$header = $this->header_check($header);
		
		$data_ret = $this->CI->send_lib->data($url,$data_arr,$header);
		
		log_message("info","data:\r\napi_url:".$url."\r\napi_data:".print_r($data_arr,true)."\r\napi_header:".print_r($header,true)."api_lib_return:\r\n".print_r($data_ret,true));
		
		if($this->return_type == "json")
		{
			$ret_arr = json_decode($data_ret,$this->is_array);
			if(is_null($ret_arr))
			{
				
				if($this->is_array)
				{
					$ret_arr = null;
					$ret_arr['type'] = 404;
					$ret_arr['message'] = $data_arr;
					$ret_arr['data']  = "";
				}
				else
				{
					$ret_arr = new stdClass();
					$ret_arr->type = 404;
					$ret_arr->message = $data_arr;
					$ret_arr->data  = "";
				}
				log_message("info","json:".print_r($ret_arr,true));
				return $ret_arr;
				
			}
			else
			{
				log_message("info","json:".print_r($ret_arr,true));
				return $ret_arr;
			}
		}
		else
		{
			log_message("info","other:".print_r($ret_arr,true));
			return $data_arr;
		}
	}

	public function site_url($type = NULL)
	{
		if( $type == NULL ){
			$this->_main_url = rtrim(get_web_url($this->api_type),"/")."/";
		}else{
			$this->_main_url = rtrim(get_web_url($type),"/")."/";
		}
	}
	
	
	private function header_check($header = array())
	{
		$header["Times"] = "Times:".time();
		
		return $header;
	}
	
    

    /**
     * Initialize preferences
     *
     * @access	public
     * @param	array
     * @return	void
     */
    function initialize($config = array())
    {
		foreach ($config as $key => $val)
		{
			if (isset($this->$key))
			{
					$this->$key = $val;
			}
		}

    }
	
}
